package com.wm.task.Task5;

import javax.sound.midi.Soundbank;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class Task {
    public static void main(String[] args) {
        Map<Character, Integer> map = new LinkedHashMap<>();
        try {
            FileReader fr = new FileReader("D:/java/study/test/c.txt");
            while (true) {
                int n = fr.read();
                char code = (char) n;
                if (n == -1) {
                    break;
                }
                if (code != '\n' && code != '\r') {
                    if (map.containsKey(code)) {
                        map.put(code, map.get(code) + 1);
                    } else {
                        map.put(code, 1);
                    }
                }

            }

            fr.close();
        } catch (FileNotFoundException e) {
            System.out.println("路径错了，找不到文件");
        } catch (IOException e) {
            e.printStackTrace();

        }
        try {
            FileWriter fw = new FileWriter("D:/java/study/test/result.txt");
            Set<Character> key = map.keySet();

            for (Character character : key) {
                fw.write(character + ":");
                fw.write(map.get(character) + "\n");
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
